package com.cdp.web.controller.system;

import com.cdp.common.core.domain.AjaxResult;
import com.cdp.common.core.domain.entity.SysStatisticsAnalysisModuleHistory;
import com.cdp.common.utils.SecurityUtils;
import com.cdp.system.dto.StatisticsAnalysisDTO;
import com.cdp.system.service.SysStatisticsAnalysisModuleHistoryService;
import com.cdp.system.vo.StatisticsAnalysisVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.hssf.record.AbstractEscherHolderRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping
@Slf4j
public class SysStatisticsAnalysisModuleHistoryController {

    @Autowired
    private SysStatisticsAnalysisModuleHistoryService sysStatisticsAnalysisModuleHistoryService;


    /**保存统计分析历史
     * @return
     */
    @PostMapping("savaStatisticsAnalysis")
    public AjaxResult  savaStatisticsAnalysis(@Validated @RequestBody StatisticsAnalysisDTO statisticsAnalysisDTO){
        String filePath = statisticsAnalysisDTO.getFilePath();
        String name = statisticsAnalysisDTO.getName();
        SysStatisticsAnalysisModuleHistory sysStatisticsAnalysisModuleHistory = new SysStatisticsAnalysisModuleHistory();
        sysStatisticsAnalysisModuleHistory.setName(name);
        sysStatisticsAnalysisModuleHistory.setFilePath(filePath);
        sysStatisticsAnalysisModuleHistory.setUserId(SecurityUtils.getUserId());
        sysStatisticsAnalysisModuleHistory.setCreateTime(LocalDateTime.now());
        sysStatisticsAnalysisModuleHistory.setDeleteTime(0);
        sysStatisticsAnalysisModuleHistory.setVersion(0);

        try {
            boolean save = sysStatisticsAnalysisModuleHistoryService.save(sysStatisticsAnalysisModuleHistory);
            return  AjaxResult.success(save);
        } catch (Exception e) {
            log.error("统计分析保存历史失败：{}",e.getMessage());
            return AjaxResult.error("统计分析保存历史失败");

        }

    }


    /**
     * 查询
     * @param statisticsAnalysisDTO
     * @return
     */
    @PostMapping("queryStatisticsAnalysis")
    public AjaxResult  queryStatisticsAnalysis( @RequestBody StatisticsAnalysisDTO statisticsAnalysisDTO){

        try {
          List<StatisticsAnalysisVO>    voList    = sysStatisticsAnalysisModuleHistoryService.queryStatisticsAnalysis(statisticsAnalysisDTO);
            return  AjaxResult.success(voList);
        } catch (Exception e) {
            log.error("统计分析查询失败：{}",e.getMessage());
            return AjaxResult.error("统计分析查询失败",e.getMessage());

        }

    }





}
